package cn.yangtianxia.org.controller;

import cn.yangtianxia.basic.util.AjaxResult;
import cn.yangtianxia.basic.util.PageList;
import cn.yangtianxia.org.domain.Employee;
import cn.yangtianxia.org.query.EmployeeQuery;
import cn.yangtianxia.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理接口
 */
@RestController
@RequestMapping("/employee")  //http://localhost:8080/dept
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    // 新增和修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() != null){
                employeeService.update(employee);
            }else{
                employeeService.save(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！");
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！");
        }
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！！");
        }
    }

    //查询
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee queryById(@PathVariable("id") Long id){
        return  employeeService.loadById(id);
    }


    //查询所有记录
    @GetMapping
    public List<Employee> queryAll(){
        return  employeeService.loadAll();
    }


    //分页和高级查询
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return  employeeService.queryPage(query);
    }


}
